<template>
    <div class="app-container">
        <avue-crud ref="crud" :data="data" :page="page" :option="tableOption"  @row-update="handleUpdate" @search-change="getList"
          @refresh-change="handleRefreshChange" @row-save="handleSave" @row-del="handleDel" @size-change="sizeChange" @current-change="currentChange">
       </avue-crud>
    </div>
</template>

<script>

import { fetchList, addObj, putObj, delObj,initPerm } from '@/api/ct_articles'
import { mapGetters } from 'vuex'

export default {
    name: 'crud',
    computed: {
        ...mapGetters(['permissions'])
    },
    data() { 
      return {
	    data:[],
		page:{ currentPage: 1,
		       total: 0,
		       pageSize: 10},
	    tableOption:{
	      "border": true,
	      "index": false,
	      "expand": false,
	      "stripe": true,
	      "selection": false,
	      "page": true,
	      "menuAlign": "center",
	      "align": "center",
	      "searchSize":"small",
	      "addBtn":false,
	      "editBtn":false,
	      "delBtn":false,
	      "column": [
	           {
		          "label": "",
		          "prop": "id",
		          "span": 12,
		          "addVisdiplay":true,
		          "editVisdiplay":true,
		          "type":"input",
		          "sortable": false,
		          "search":false,
		          "hide":undefined,
		          "rules": [{
			        "required": true,
			        "message": "请输入",
			        "trigger": "blur"
		          }]
		        },
	           {
		          "label": "文章栏目",
		          "prop": "categoryId",
		          "span": 12,
		          "addVisdiplay":true,
		          "editVisdiplay":true,
		          "type":"input",
		          "sortable": false,
		          "search":false,
		          "hide":undefined,
		          "rules": [{
			        "required": true,
			        "message": "请输入文章栏目",
			        "trigger": "blur"
		          }]
		        },
	           {
		          "label": "标题",
		          "prop": "title",
		          "span": 12,
		          "addVisdiplay":true,
		          "editVisdiplay":true,
		          "type":"input",
		          "sortable": false,
		          "search":false,
		          "hide":undefined,
		          "rules": [{
			        "required": true,
			        "message": "请输入标题",
			        "trigger": "blur"
		          }]
		        },
	           {
		          "label": "简要介绍",
		          "prop": "summary",
		          "span": 12,
		          "addVisdiplay":true,
		          "editVisdiplay":true,
		          "type":"input",
		          "sortable": false,
		          "search":false,
		          "hide":undefined,
		          "rules": [{
			        "required": true,
			        "message": "请输入简要介绍",
			        "trigger": "blur"
		          }]
		        },
	           {
		          "label": "缩略图",
		          "prop": "thumbs",
		          "span": 12,
		          "addVisdiplay":true,
		          "editVisdiplay":true,
		          "type":"input",
		          "sortable": false,
		          "search":false,
		          "hide":undefined,
		          "rules": [{
			        "required": true,
			        "message": "请输入缩略图",
			        "trigger": "blur"
		          }]
		        },
	           {
		          "label": "文章来源",
		          "prop": "source",
		          "span": 12,
		          "addVisdiplay":true,
		          "editVisdiplay":true,
		          "type":"input",
		          "sortable": false,
		          "search":false,
		          "hide":undefined,
		          "rules": [{
			        "required": true,
			        "message": "请输入文章来源",
			        "trigger": "blur"
		          }]
		        },
	           {
		          "label": "作者",
		          "prop": "author",
		          "span": 12,
		          "addVisdiplay":true,
		          "editVisdiplay":true,
		          "type":"input",
		          "sortable": false,
		          "search":false,
		          "hide":undefined,
		          "rules": [{
			        "required": true,
			        "message": "请输入作者",
			        "trigger": "blur"
		          }]
		        },
	           {
		          "label": "内容",
		          "prop": "contents",
		          "span": 12,
		          "addVisdiplay":true,
		          "editVisdiplay":true,
		          "type":"input",
		          "sortable": false,
		          "search":false,
		          "hide":undefined,
		          "rules": [{
			        "required": true,
			        "message": "请输入内容",
			        "trigger": "blur"
		          }]
		        },
	           {
		          "label": "标签",
		          "prop": "tags",
		          "span": 12,
		          "addVisdiplay":true,
		          "editVisdiplay":true,
		          "type":"input",
		          "sortable": false,
		          "search":false,
		          "hide":undefined,
		          "rules": [{
			        "required": true,
			        "message": "请输入标签",
			        "trigger": "blur"
		          }]
		        },
	           {
		          "label": "点击数",
		          "prop": "hits",
		          "span": 12,
		          "addVisdiplay":true,
		          "editVisdiplay":true,
		          "type":"input",
		          "sortable": false,
		          "search":false,
		          "hide":undefined,
		          "rules": [{
			        "required": true,
			        "message": "请输入点击数",
			        "trigger": "blur"
		          }]
		        },
	           {
		          "label": "添加人aid",
		          "prop": "addAdminId",
		          "span": 12,
		          "addVisdiplay":true,
		          "editVisdiplay":true,
		          "type":"input",
		          "sortable": false,
		          "search":false,
		          "hide":undefined,
		          "rules": [{
			        "required": true,
			        "message": "请输入添加人aid",
			        "trigger": "blur"
		          }]
		        },
	           {
		          "label": "添加时间",
		          "prop": "addTime",
		          "span": 12,
		          "addVisdiplay":true,
		          "editVisdiplay":true,
		          "type":"input",
		          "sortable": false,
		          "search":false,
		          "hide":undefined,
		          "rules": [{
			        "required": true,
			        "message": "请输入添加时间",
			        "trigger": "blur"
		          }]
		        },
	           {
		          "label": "",
		          "prop": "updAdminId",
		          "span": 12,
		          "addVisdiplay":true,
		          "editVisdiplay":true,
		          "type":"input",
		          "sortable": false,
		          "search":false,
		          "hide":undefined,
		          "rules": [{
			        "required": true,
			        "message": "请输入",
			        "trigger": "blur"
		          }]
		        },
	           {
		          "label": "更新时间",
		          "prop": "updTime",
		          "span": 12,
		          "addVisdiplay":true,
		          "editVisdiplay":true,
		          "type":"input",
		          "sortable": false,
		          "search":false,
		          "hide":undefined,
		          "rules": [{
			        "required": true,
			        "message": "请输入更新时间",
			        "trigger": "blur"
		          }]
		        },
	           {
		          "label": "排序",
		          "prop": "sortIndex",
		          "span": 12,
		          "addVisdiplay":true,
		          "editVisdiplay":true,
		          "type":"input",
		          "sortable": false,
		          "search":false,
		          "hide":undefined,
		          "rules": [{
			        "required": true,
			        "message": "请输入排序",
			        "trigger": "blur"
		          }]
		        },
	           {
		          "label": "文章状态 1正式发布 -1草稿",
		          "prop": "status",
		          "span": 12,
		          "addVisdiplay":true,
		          "editVisdiplay":true,
		          "type":"input",
		          "sortable": false,
		          "search":false,
		          "hide":undefined,
		          "rules": [{
			        "required": true,
			        "message": "请输入文章状态 1正式发布 -1草稿",
			        "trigger": "blur"
		          }]
		        },
	      ]
	    },
      }
    },
    created(){
    	this.getList()
    	initPerm(this.tableOption,this.permissions)
    },
    methods: {
	    handleDel: function(row, index) {
	        var _this=this;
	        
	        this.$confirm("是否确认删除记录" , "提示", {
			    confirmButtonText: "确定",
			    cancelButtonText: "取消",
			    type: "warning"
	        })
	        .then(function() {
	          delObj(row).then(response => {
	        	_this.getList()
		        _this.$notify({
		          title: '成功',
		          message: '删除成功',
		          type: 'success',
		          duration: 2000
		        })
	          })
	        })
	        .catch(function(err) {});
	    },
	    handleUpdate: function(row, index, done) {
	    	var _this=this;
	        putObj(row).then(() => {
	        _this.getList()
	        _this.$notify({
			    title: '成功',
			    message: '修改成功',
			    type: 'success',
			    duration: 2000
	          })
	        })
	        done();
	    },
	    handleSave: function(row, done) {
	      var _this=this;
	      addObj(row).then(() => {
		        _this.dialogFormVisible = false
		        _this.getList()
		        _this.$notify({
			        title: '成功',
			        message: '创建成功',
			        type: 'success',
			        duration: 2000
	        })
	      })
	      done();
	    },
	    getList(form){
	      if(!form&&this.$refs.crud)
	        form=this.$refs.crud.searchForm
	      fetchList(Object.assign({}, form, { page: `${this.page.currentPage}`,limit:`${this.page.pageSize}` }))
	        .then(response => {
			    this.data = response.data.records
			    this.page.total = response.data.total
			    this.page.currentPage = response.data.current
	      })
	    },
	    handleRefreshChange(page) {
	      this.getList();
	    },
	    sizeChange(val) {
	        this.page.currentPage = 1
	        this.page.pageSize = val
	        this.getList()
	    },
	    currentChange(val) {
	        this.page.currentPage = val
	        this.getList()
	    }
    }
}
</script>
